package TuiLayer;
import java.util.Scanner;
import ControlLayer.*;
/**TuiLayer package sets the location for MainMenuUI class.
 * 
 * Scanner package is used here, because it provides tracking on the inputs from a
 * keyboard. This gives User(Private Person) the ability to have control on 
 * MainMenuUI. The control is exercised by inserting different values through the 
 * keyboard. This means that MainMenuUI behaves strictly in accordance to the user 
 * preferences.
 * 
 * The ControlLayer package is imported, because TUIs must be able to access classes
 * in the ControlLayer - AddressController, RegisterController and LoanController.
 * ---------------------------------------------------------------------------------
 * MainMenuUI class is devided in five sections. In each section(except the first
 * one) methods with similar functionalities are placed.
 * 
 * Section Contents:
 *  Variables(No methods)
 *  Constructors
 *      - MainMenuUI()
 *  UserTrackers
 *      - startMainMenu()
 *      - writeMainMenu()
 *  Methods
 *      - startAddressBook()
 *      - startDVD()
 *      - startLoan()
 *  Printers
 *      - writeEnd()
 *      - writeError()
 */ 

public class MainMenuUI
{
  /** VARIABLES */
    //In order MainMenuUI to be able to launch AddressBookUI, DVDUI, LoanUI the 
    //following object references (associations) are needed. They provide access to
    //the mentioned classes. 
    private AddressBookUI adrUI;    //Reference to AdressBookUI.
    private DVDUI dvdUI;            //Reference to DVDUI.
    private LoanUI loanUI;          //Reference to LoanUI.
   
  /** CONSTRUCTORS */
  public MainMenuUI()
  //Creates an instance of MainMenuUI class.
  {
       startMainMenu(); //See User Tracker section.
  }
  
  /** USER TRACKERS */
  public void startMainMenu()
  //This can be considered as the UI launcher, because it launches the interface
  //for MainMenu and initializes the optionChoice functionality. User is
  //now able to use the interface.
  {
      boolean isRunning = true;
      //This indicates whether the UI is launched and running.
      
      while (isRunning)
      //This can be interpreted like this: 'While the application is running
      //perform the following tasks below'. If the boolean expression isRunning
      //becomes false, then stops working and the application is closed.
      //User cannot use the system anymore.
      {
          int optionChoice = writeMainMenu();
          //Before determing what the UI have to execute, it will acquire
          //user optionChoice first, by calling this method.
          
          if(optionChoice == 1)
          //User inputs 1 as an optionChoice - User is redirected to 
          //AddressBook Menu.
          {
              startAddressBook();   //UI launches AddressBookUI.
          }
          
          else if(optionChoice == 2)
          //User inputs 2 as an optionChoice - User is redirected to DVD Menu.
          {
              startDVD();   //UI launches DVDUI.
          }
          
          else if(optionChoice == 3)
          //User inputs 3 as an optionChoice - User is redirected to Loan Menu
          {
              startLoan();  //UI launches LoanUI.
          }
          
          else if(optionChoice == 0)
          //User inputs 0 as an optionChoice - User exits the system.
          {
              writeEnd();
              //Displays a message that the system is shut down.
              isRunning = false;
              //Variable isRunning becomes false. UI shuts down the system and
              //application is terminated.
          } 
          
          else if(optionChoice != 1 && optionChoice != 2 && optionChoice != 3
                  && optionChoice != 0)
          //This checks whether the optionChoice number is a valid command. User 
          //makes invalid input(the number is not within the range from 0 to 3).
          //A message is displayed to notify about the problem.
          {
              Scanner keyboard = new Scanner(System.in);
              writeError(); //Displays an error message.
              String code = keyboard.nextLine();
              //Prompts user to press Enter to continue with using the UI.
          }
        
      }
  }
    
  private int writeMainMenu()
  //Acquires optionChoice. It creates a keyboard object(of Scanner class), so 
  //that user can input her optionChoice. It is important to note that this
  //method also visualizes the UI.
  {      
      Scanner keyboard = new Scanner(System.in);
      //Creates the keyboard object.
      
      System.out.println("\f *** Main Menu ***");
      System.out.println(" (1) Address Book");
      System.out.println(" (2) DVDs");
      System.out.println(" (3) Loan");
      System.out.println(" (0) Close");
      System.out.print("\n Choose an option: ");
      //MainMenuUI visualization. It is made by printing these lines in the
      //Terminal window.
      
      String checkInput = keyboard.nextLine();
      //Tracks user inputs by calling Scanner nextLine() method on each input.
      
      int optionChoice; //Local variable, where user option choice will be stored.
      
      try
      //After getting user input, this try-catch structure checks whether the
      //input is a number(integer). This allows the use of integers, instead of 
      //strings for determing the user option choice and at the same time this
      //prevents system from crashing.
      {
          optionChoice = Integer.parseInt(checkInput);
          //If it is a number then UI gets an optionChoice to check whether it
          //there is such an option.
      }
      
      catch(NumberFormatException wrongInput)
      {
          optionChoice = -1;
          //If the input is not a number, then this code will make UI display a
          //message, to notify user about the problem.
      }
      
      return optionChoice; //Sends what is the user optionChoice.
  }
   
  /** METHODS */
  private void startAddressBook()
  //This method launches AddressBookUI. User is redirected to AddressBookUI.
  {
      adrUI= new AddressBookUI();
      adrUI.start();
  }
  
  private void startDVD()
  //This method launches DVDUI. User is redirected to DVDUI.
  {
      dvdUI = new DVDUI();
      dvdUI.start();
  }
  
  private void startLoan()
  //This method launches LoanUI. User is redirected to LoanUI.
  {
      loanUI = new LoanUI();
      loanUI.start();
  }
  
  /** PRINTERS */
  private void writeEnd()
  //Prints an end message in the terminal window.
  {
       System.out.println(" The system shut down.");
  }
  
  private void writeError()
  //Prints an error message in the terminal window.
  {
      System.out.println("\n [ERROR]:\n You can only insert\n numbers 1, 2, 3 and"
                                + " 0\n for an option choice."
                                + "\n\n Press Enter to continue.");
  }
}
